/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var threeSum = function (nums) {
  // 排序 减少遍历次数,方便去掉重复情况
  nums.sort((a, b) => a - b)
  let n = nums.length
  let res = []
  let s, e, target
  for (let i = 0; i < n; i++) {
    if (i > 0 && nums[i] === nums[i - 1]) {
      continue
    }
     target = 0 - nums[i]
     s = i + 1,e = n - 1
    while (s < e) {
      if (s > i + 1 && nums[s] === nums[s - 1]) {
        s++
      } else if (e < n - 1 && nums[e] === nums[e + 1]) {
        e--
      } else {
        if (nums[s] + nums[e] > target) {
          e--
        } else if (nums[s] + nums[e] < target) {
          s++
        } else {
          res.push([nums[i], nums[s], nums[e]])
          s++
          e--
        }
      }
    }

  }
  return res

};
var nums = [-1, 0, 1, 2, -1, -4]
console.log(threeSum(nums))
